#!/bin/bash
#
# Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Streaming - User space tracing"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../..
NR_ITER=1
NR_USEC_WAIT=0
TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
SESSION_NAME="stream"
EVENT_NAME="tp:tptest"

TRACE_PATH=$(mktemp -d -t tmp.test_regen_metadata_ust_trace_path.XXXXXX)

NUM_TESTS=47

source $TESTDIR/utils/utils.sh

if [ ! -x "$TESTAPP_BIN" ]; then
	BAIL_OUT "No UST events binary detected."
fi

function enable_channel_per_pid()
{
	sess_name=$1
	channel_name=$2

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel --buffers-pid -u $channel_name -s $sess_name >/dev/null 2>&1
	ok $? "Enable channel $channel_name per PID for session $sess_name"
}

# MUST set TESTDIR before calling those functions

function test_ust_streaming ()
{
	local file_sync_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.XXXXXX")
	local file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	local metadata_path

	diag "Test UST streaming with metadata regeneration"
	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-after-first-event ${file_sync_after_first} \
		--sync-before-last-event ${file_sync_before_last} >/dev/null 2>&1 &

	start_lttng_tracing_ok $SESSION_NAME

	touch ${file_sync_before_last}

	# Wait for the applications started in background
	wait

	# Expect a valid trace
	stop_lttng_tracing_ok $SESSION_NAME
	validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$SESSION_NAME"*

	# Find the metadata file
	metadata_path=$(find "$TRACE_PATH/$HOSTNAME/$SESSION_NAME"* -name "metadata")

	# Empty the metadata file
	echo -n > "$metadata_path"

	start_lttng_tracing_ok $SESSION_NAME
	regenerate_metadata_ok $SESSION_NAME
	stop_lttng_tracing_ok $SESSION_NAME

	# Metadata file must have been regenerated
	validate_trace $EVENT_NAME $TRACE_PATH/$HOSTNAME/$SESSION_NAME*
	destroy_lttng_session_ok $SESSION_NAME

	rm -rf "$TRACE_PATH"
	rm -f ${file_sync_after_first}
	rm -f ${file_sync_before_last}
}

function test_ust_local ()
{
	local file_sync_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.XXXXXX")
	local file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	local metadata_path

	diag "Test UST local with metadata regeneration"
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-after-first-event ${file_sync_after_first} \
		--sync-before-last-event ${file_sync_before_last} > /dev/null 2>&1 &

	start_lttng_tracing_ok $SESSION_NAME

	touch ${file_sync_before_last}

	# Wait for the applications started in background
	wait

	# Expect a valid trace
	stop_lttng_tracing_ok $SESSION_NAME
	validate_trace $EVENT_NAME "$TRACE_PATH"

	# Find the metadata file
	metadata_path=$(find "$TRACE_PATH"* -name "metadata")

	# Empty the metadata file
	echo -n > "$metadata_path"

	start_lttng_tracing_ok $SESSION_NAME
	regenerate_metadata_ok $SESSION_NAME
	stop_lttng_tracing_ok $SESSION_NAME

	# Metadata file must have been regenerated
	validate_trace $EVENT_NAME $TRACE_PATH
	destroy_lttng_session_ok $SESSION_NAME

	rm -rf "$TRACE_PATH"
	rm -f ${file_sync_after_first}
	rm -f ${file_sync_before_last}
}

function test_ust_pid ()
{
	local file_sync_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.XXXXXX")
	local file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")

	diag "Test UST per-pid with metadata regeneration (expect failure)"
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME "channel0"

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-after-first-event ${file_sync_after_first} \
		--sync-before-last-event ${file_sync_before_last} >/dev/null 2>&1 &

	start_lttng_tracing_ok $SESSION_NAME

	touch ${file_sync_before_last}

	# Wait for the applications started in background
	wait
	regenerate_metadata_fail $SESSION_NAME

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME
	# Validate test
	validate_trace $EVENT_NAME $TRACE_PATH
	if [ $? -eq 0 ]; then
		# Only delete if successful
		rm -rf $TRACE_PATH
	fi
	rm -f ${file_sync_after_first}
	rm -f ${file_sync_before_last}
}

function test_ust_live ()
{
	local file_sync_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.XXXXXX")
	local file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")

	diag "Test UST live with metadata regeneration (expect failure)"
	create_lttng_session_uri $SESSION_NAME net://localhost --live
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-after-first-event ${file_sync_after_first} \
		--sync-before-last-event ${file_sync_before_last} >/dev/null 2>&1 &

	start_lttng_tracing_ok $SESSION_NAME

	touch ${file_sync_before_last}

	# Wait for the applications started in background
	wait
	regenerate_metadata_fail $SESSION_NAME

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME
	# Validate test
	validate_trace $EVENT_NAME $TRACE_PATH
	if [ $? -eq 0 ]; then
		# Only delete if successful
		rm -rf $TRACE_PATH
	fi
	rm -f ${file_sync_after_first}
	rm -f ${file_sync_before_last}
}

function test_ust_local_snapshot_after_regenerate_metadata ()
{
	local file_sync_before_last
	local metadata_path

	file_sync_before_last=$(mktemp -u)

	diag "Test UST local snapshot after regenerate metadata"
	create_lttng_session_ok $SESSION_NAME "$TRACE_PATH" --snapshot
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME
	start_lttng_tracing_ok $SESSION_NAME

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT

	regenerate_metadata_ok $SESSION_NAME
	lttng_snapshot_record $SESSION_NAME "$TRACE_PATH"
	stop_lttng_tracing_ok $SESSION_NAME

	validate_trace $EVENT_NAME "$TRACE_PATH"
	destroy_lttng_session_ok $SESSION_NAME

	rm -rf "$TRACE_PATH"
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"
bail_out_if_no_babeltrace

start_lttng_relayd "-o $TRACE_PATH"
start_lttng_sessiond

tests=( test_ust_streaming test_ust_local test_ust_pid test_ust_live
	test_ust_local_snapshot_after_regenerate_metadata)

for fct_test in ${tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test}
done

stop_lttng_sessiond
stop_lttng_relayd

exit $out
